package org.to_zero.springboot.jwt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.to_zero.springboot.jwt.entity.SysUser;
import org.to_zero.springboot.jwt.mapper.SysUserMapper;
import org.to_zero.springboot.jwt.service.IAdminService;
import org.to_zero.springboot.jwt.uitl.JwtUtil;

import javax.annotation.Resource;

@Service
public class AdminServiceImpl implements IAdminService {
    @Resource
    private SysUserMapper userMapper;
    @Resource
    private JwtUtil jwtUtil;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Override
    public String login(SysUser user) {
        String username = user.getUsername();
        String password = user.getPassword();
        Assert.hasLength(user.getUsername(),"用户名不能为空");
        Assert.hasLength(user.getPassword(),"密码不能为空");
        QueryWrapper<SysUser> wrapper = new QueryWrapper<SysUser>().eq("username", username);
        SysUser sysUser = userMapper.selectOne(wrapper);
        Assert.isTrue(passwordEncoder.matches(password,sysUser.getPassword()),"用户名或密码有误");
        return jwtUtil.generateToken(sysUser);
    }
}
